﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wq.Extensions;
using Wq.Extensions.Repository;
using Wq.ManagerService.Entities;
using Wq.ManagerService.Models;
using Wq.Model;



namespace Wq.ManagerService
{
    /// <summary>
    /// 地区管理
    /// </summary>
    public class RegionService : SqlSugarBaseRepository<RegionInfo>, IAddSingleton
    {
        private readonly RedisBaseRepository redisRepository;
        public RegionService(RedisBaseRepository redisRepository) => this.redisRepository = redisRepository;

        /// <summary>
        /// 获取全部数据
        /// </summary>
        public List<RegionData> list(int maxLevel = 4) => redisRepository.Get(SysCacheKey.Regions(maxLevel), () => Context.Queryable<RegionInfo>().Where(p => p.Level <= maxLevel).Select<RegionData>().ToList()).Result;

        /// <summary>
        /// 获取下级集合
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public List<RegionData> Subs(int parentId) =>
            Context.Queryable<RegionInfo>()
            .Where(p => p.ParentId == parentId).Select<RegionData>()
            .Mapper(p => {
                p.hasChildren = Context.Queryable<RegionInfo>().Where(r => r.ParentId == p.id).Count() > 0;
            }).ToList();


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="name"></param>
        /// <param name="parentId"></param>
        public void Create(string name, int parentId)
        {
            var info = Context.Queryable<RegionInfo>().First(p => p.Id == parentId);
            Context.Insertable(new RegionInfo { Name = name, ParentId = parentId, Level = info?.Level ?? 1 }).ExecuteCommand();
            RemoveCache();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        public void Save(int id, string name)
        {
            Context.Updateable<RegionInfo>().SetColumns(p => p.Name == name).Where(p => p.Id == id).ExecuteCommand();
            RemoveCache();

        }

        /// <summary>
        /// 移除缓存
        /// </summary>
        public async void RemoveCache()
        {
            for (int i = 1; i <= 4; i++)
            {
                await redisRepository.Remove(SysCacheKey.Regions(i));
                await redisRepository.Remove(SysCacheKey.RegionToLevel(i));
            }
        }

        /// <summary>
        /// 获取全路径
        /// </summary>
        /// <param name="regionId"></param>
        /// <param name="split"></param>
        /// <returns></returns>
        public string GetFullName(long regionId, string split = " ")
        {
            var parent = list(4).Where(p => p.id == regionId).FirstOrDefault();
            if (parent != null)
                return $"{GetFullName(parent.parentId,split)}{parent.name}{split}";
            return string.Empty;
        }

        /// <summary>
        /// 获取全路径Id
        /// </summary>
        /// <param name="regionId"></param>
        /// <param name="split"></param>
        /// <returns></returns>
        public string GetFullIds(long regionId, string split = " ")
        {
            var parent = list(4).Where(p => p.id == regionId).FirstOrDefault();
            if (parent != null)
                return $"{GetFullIds(parent.parentId,split)}{parent.id}{split}";
            return string.Empty;
        }
    }
}
